package com.ruicar.afs.cloud.gateway.handler.loginprocess;

import com.ruicar.afs.cloud.common.core.constant.CommonConstants;
import com.ruicar.afs.cloud.common.core.exception.AfsBaseException;
import com.ruicar.afs.cloud.common.core.security.feign.Oauth2OperationService;
import com.ruicar.afs.cloud.common.core.util.ClientTypeUtil;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

@Slf4j
@Component
@AllArgsConstructor
public class AppLoginProcess implements LoginProcess{

    private final Oauth2OperationService oauth2OperationService;
    @Override
    public List<ClientTypeUtil.ClientTypeEnum> supportClientType() {
        return Arrays.asList(ClientTypeUtil.ClientTypeEnum.APP_IOS,ClientTypeUtil.ClientTypeEnum.APP_AN);
    }

    @Override
    public Oauth2OperationService getOauthClient() {
        return oauth2OperationService;
    }

    @Override
    public void checkHeader(Map<String, String> headers) {
        if(headers.keySet().stream().filter(s -> s.equalsIgnoreCase(CommonConstants.DEVICE_ID)).count()==0){
            throw new AfsBaseException("不允许的客户端");
        }else{
            headers.put(CommonConstants.DEVICE_ID,headers.get(headers.keySet().stream().filter(s -> s.equalsIgnoreCase(CommonConstants.DEVICE_ID)).findFirst().toString()));
        }
        headers.put("tenant-id", "system");
    }

    @Override
    public void checkBody(Map<String, String> bodyParams) {

    }
}
